import os
import json
import re


def report_csv(files):
  files.sort(reverse=True)
  files = files[0:5]

  repo_lists = []
  for file in files:
    with open(file, 'r') as f:
      repo_lists.append(json.load(f))

  lines = [[''] * (3 * len(repo_lists) + 2) for _ in range(501)]

  for index, repo_list in enumerate(repo_lists):
    date_str = files[index].split('/')[-1].split('.')[0]
    if index == 0:
      lines[0][0] = f'{date_str}_repo_url'
      lines[0][1] = f'{date_str}_repo_tags'
    col_idx = 2 + 3 * index
    lines[0][col_idx] = date_str
    lines[0][col_idx + 1] = 'rank'
    lines[0][col_idx + 2] = 'rankmov'
    base_list = repo_lists[min(index + 1, len(repo_lists) - 1)]
    base_dict = {repo['full_name']: repo for repo in base_list}

    for repo_index, repo in enumerate(repo_list):
      lineno = repo_index + 1

      name = repo['full_name']
      rank = repo['sort_index']

      rank_mov = ''
      if name in base_dict:
        mov = base_dict[name]['sort_index'] - rank
        if mov > 0:
          rank_mov = f':+{mov}'
        elif mov < 0:
          rank_mov = f':{mov}'
      else:
        rank_mov = f':+{500-rank}+'

      if index == 0:
        lines[lineno][0] = repo['html_url']
        tags = repo['tags'].split(',')
        tags = sorted(tags, key=lambda x: 3 * len(re.findall('[aeiouAEIOU]', x)) + len(x))
        tags = tags[0:4]
        lines[lineno][1] = '|'.join(tags)
      lines[lineno][col_idx] = name
      lines[lineno][col_idx + 1] = str(rank + 1)
      lines[lineno][col_idx + 2] = rank_mov
    
    with open('data/report.csv', 'w') as f:
      f.write('\n'.join([','.join(line) for line in lines]))


if __name__ == "__main__":
  files = os.listdir('data/populars')
  report_csv([f'data/populars/{file}' for file in files if re.match(r"^\d{4}_\d{2}_\d{2}\.json$", file)])